<?php


namespace app\api\controller;

use think\facade\Db;
use app\common\model\Config;
//公共类
class Common extends ApiBase
{
    //上传，检测非法图片
    public function upload()
    {

        $file = request()->file("Image");

        $uploadType = Db::name("system")->where("config", "uploadType")->value("value");

        $wechat = new Wechat();

        if ($uploadType == 1) {
            //上传本地服务器
            try {
                $path = request()->domain() . "/" . \think\facade\Filesystem::disk('public')->putFile('uploads/postImages', $file);
                $savename = str_replace("\\", "/", $path);

                if ($this->imgCheck($savename) != 0) {
                    return error("内容违法违规");
                }

                return success(["url" => $savename], "上传成功");
            } catch (\think\exception\ValidateException $e) {
                return error($e->getMessage());
            }
        } elseif ($uploadType == 2) {
            //上传阿里云OSS
            $res = alYunOSS($file, $file->extension());

            if ($res["code"] === 1) {
                $savename = str_replace("\\", "/", $res["url"]);
            } else {
                return error($res["msg"]);
            }

            if ($this->imgCheck($savename) != 0) {
                return error("内容违法违规");
            }

            return success(["url" => $savename], "上传成功");
        }
    }

    //上传，不检测
    public function uploadFile()
    {

        $file = request()->file("file");

        $uploadType = Db::name("system")->where("config", "uploadType")->value("value");
        if ($uploadType == 1) {
            //上传本地服务器
            try {
                $path = request()->domain() . "/" . \think\facade\Filesystem::disk('public')->putFile('uploads/postImages', $file);
                $savename = str_replace("\\", "/", $path);

                return success(["url" => $savename], "上传成功");
            } catch (\think\exception\ValidateException $e) {
                return error($e->getMessage());
            }
        } elseif ($uploadType == 2) {
            //上传阿里云OSS
            $res = alYunOSS($file, $file->extension());

            if ($res["code"] === 1) {
                $savename = str_replace("\\", "/", $res["url"]);
            } else {
                return error($res["msg"]);
            }

            return success(["url" => $savename], "上传成功");
        }
    }

    //获取系统配置
    public function getConfig(){
        $name = input("name");
        $config = Config::where("name",$name)->find();

        return success($config);
    }

    //修改系统配置
    public function setConfig(){
        $name = input("name");
        $value = input("config_value");

        $res = Config::update(["name" => $name,"config_value" => $value]);

        if($res){
            return notice("保存成功");
        }

        return error("保存失败");
    }
}